/**
 * @author yxw
 * date : 2013年10月17日 下午3:00:30 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.ImagePagerAdapter;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.activity.view.VerticalScrollView;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.NewhouseHouseType;
import com.tzinfo.tzt.domain.NewhouseHouseTypeItem;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;

public class NewHouseTypeListActivity extends Activity implements
		OnClickListener, OnPageChangeListener {
	// data
	private MyResultList newHouseHouseTypeResult;
	private NewhouseHouseType newhouseHouseType;
	private List<NewhouseHouseTypeItem> houseTypeItemList;

	// Util
	private ImagePagerAdapter housePicPagerAdapter;
	private List<Map<String, Object>> houseTypeData;
	private Dialog dialog;
	private Intent thisIntent;
	private String sHouseId = "";
	private int position = 0;

	// UI组件
	private ViewPager roomTypePager;
	private Button backBtn;
	private VerticalScrollView mScrollView;
	private TextView[] textView = new TextView[6];


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		thisIntent = this.getIntent();
		sHouseId = thisIntent.getStringExtra("houseId");
		position = thisIntent.getIntExtra("position", 0);
		setContentView(R.layout.activity_house_type_pager);
		init();
	}

	private void init() {
		roomTypePager = (ViewPager) this.findViewById(R.id.pager_room_type);
		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		// 加载进度条
		dialog.show();
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		mScrollView = (VerticalScrollView) this.findViewById(R.id.scrollview);

		// textView[0]=(TextView)this.findViewById(R.id.text_HousePic_TypeName);
		textView[1] = (TextView) this.findViewById(R.id.text_HousePic_NO);
		textView[2] = (TextView) this.findViewById(R.id.text_HousePic_Name);
		textView[3] = (TextView) this.findViewById(R.id.text_HousePic_Size);
		textView[4] = (TextView) this
				.findViewById(R.id.text_HousePic_Direction);
		textView[5] = (TextView) this.findViewById(R.id.text_HousePic_Content);
		new Thread(getNewHouseDetailRun).start();

	}

	private void updateUI() {
		housePicPagerAdapter = new ImagePagerAdapter(this, houseTypeData,
				R.layout.item_pager_room_type_detail, new String[] { "ID",
						"Pic", }, new int[] { R.id.un_HousePic_id,
						R.id.img_HousePic_pic, });

		roomTypePager.setAdapter(housePicPagerAdapter);
		if(position+1>houseTypeData.size()){
			position=0;
		}
		roomTypePager.setCurrentItem(position);
		roomTypePager.setOnPageChangeListener(this);
		mScrollView.smoothScrollTo(0, 0);
		setUIContent();
	}

	private void setUIContent() {
		NewhouseHouseTypeItem houseTypeItem = houseTypeItemList.get(position);
		// textView[0].setText(houseTypeItemList.get(position).getTypeName());
		textView[1].setText(houseTypeItem.getNo());
		textView[2].setText(houseTypeItem.getName());
		textView[3].setText(houseTypeItem.getSize() + "平米");
		textView[4].setText(houseTypeItem.getDirection());
		String content = houseTypeItem.getContent();
		if (content == null || "".equals(content)|| "null".equals(content)) {
			content = "当前没有户型描述信息！";
		}
		textView[5].setText(content);
	}

	// 得到data数据
	private void getData() {
		houseTypeData = new ArrayList<Map<String, Object>>();

		Map<String, Object> map;
		if (newHouseHouseTypeResult.getSuccess()) {
			Log.e("size", newHouseHouseTypeResult.getData().size() + "");
			newhouseHouseType = (NewhouseHouseType) newHouseHouseTypeResult
					.getData().get(0);
			houseTypeItemList = newhouseHouseType.getList();
			NewhouseHouseTypeItem item;
			if (houseTypeItemList != null) {
				for (int i = 0; i < houseTypeItemList.size(); i++) {
					map = new HashMap<String, Object>();
					item = houseTypeItemList.get(i);
					map.put("TypeName", item.getTypeName());
					map.put("ID", item.getID() + "");
					map.put("Pic", item.getPic());
					map.put("Name", item.getName().trim());
					map.put("No", item.getNo().trim());
					map.put("Size", item.getSize() + "平米");
					map.put("Direction", item.getDirection().trim());
					map.put("Content", item.getContent().trim());
					houseTypeData.add(map);
				}
			}
		}

	}

	/**
	 * 线程 得到新房列表
	 */
	Runnable getNewHouseDetailRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			jsonString = HttpTools.getJsonContent(AppDataConfig.xinfangUrl
					+ AppDataConfig.NewhouseHouseType + "?houseid=" + sHouseId);
			Log.e("JsonString", jsonString);
			newHouseHouseTypeResult = JsonTools
					.getNewhouseHouseType(jsonString);
			getData();
			if (newHouseHouseTypeResult.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = newHouseHouseTypeResult.getErrorMessage();
			}
			handler.sendMessage(msg);
		}
	};

	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(getNewHouseDetailRun).start();
					}
				}).show();
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				updateUI();
				dialog.dismiss();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			case 5:
				break;
			case 7:
				break;
			case 0:
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;

		default:
			break;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		position = roomTypePager.getCurrentItem();
		setUIContent();

	}
}
